//
//  PPChangePasswordViewController.m
//  pet
//
//  Created by 陈无忌 on 1/14/15.
//  Copyright (c) 2015 whalefin. All rights reserved.
//

#import "PPChangePasswordViewController.h"
#import "PPSystemConfiguraton.h"
#import "supportingHeadFiles.h"

@interface PPChangePasswordViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *oldPasswordTF;
@property (weak, nonatomic) IBOutlet UITextField *setPasswordTF;
@property (weak, nonatomic) IBOutlet UITextField *checkPasswordTF;

@property (weak, nonatomic) PPSystemConfiguraton *config;

@end

@implementation PPChangePasswordViewController
- (PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.oldPasswordTF.delegate = self;
    self.setPasswordTF.delegate = self;
    self.checkPasswordTF.delegate = self;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)confirm:(id)sender {
    //check
    if (![self.setPasswordTF.text isEqualToString:self.checkPasswordTF.text]) {
        [PPHUD hudShowWithString:@"两次密码输入不一致" andView:self.view];
        return;
    }
    if (self.setPasswordTF.text.length < 6) {
        [PPHUD hudShowWithString:@"请输入不少于6位的密码" andView:self.view];
        return;
    }
    NSString *url = [PPAPIList getAPIList].PPChangePassword;
    NSDictionary *params = @{@"userId":@(self.config.userInfo.id),@"oldPassword":self.oldPasswordTF.text,@"newPassword":self.setPasswordTF.text};
    [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber *processResult = responseObject[@"processResult"];
        NSString *errorMsg = responseObject[@"errorMsg"];
        if (processResult.boolValue) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [PPHUD hudShowWithString:errorMsg andView:self.view];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [PPHUD hudShowWithString:@"网络不给力" andView:self.view];
    }];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self.oldPasswordTF isExclusiveTouch]) {
        [self.oldPasswordTF resignFirstResponder];
    }
    if (![self.setPasswordTF isExclusiveTouch]) {
        [self.setPasswordTF resignFirstResponder];
    }
    if (![self.checkPasswordTF isExclusiveTouch]) {
        [self.checkPasswordTF resignFirstResponder];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
